import { nailerService } from './../../providers/nailerService';
import { Component, Injector } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { BasePage } from '../basePage';
// import { NativeService } from '../../providers/NativeService';
import { StorageService } from './../../providers/StorageService';
import { Events } from 'ionic-angular';
import { PIC_URL } from "./../../providers/Config";




@IonicPage()
@Component({
  selector: 'page-choiceness',
  templateUrl: 'choiceness.html',
})
export class ChoicenessPage extends BasePage {
  picrooturl = '';
  oboaut: any;

  nails = [];
  /**提示信息 */
  msg: string = "";

  userinfo: any;
  // oboaut = false;
  /**获取美甲师列表参数 */
  getNailerList = {
    local_city: "420100",
    artist_type: 1,
    user_id: null,
    page_size:3
  }
  /**接收美甲师信息 */
  nailerList = [];
  /**是否关注 */
  is_focus = '';
  /**接收导航栏的状态 */
  status: number;
  /**关注参数 */
  focusNailerObj = {
    focused_id: '',
    user_id: 0
  }
  btm:boolean=false
  haveId:boolean;  //无身份隐藏悬浮按钮
  constructor(injector: Injector, 
    private nailerservice: nailerService, 
    public storage: StorageService, 
    public events: Events) {
    super(injector);
    this.picrooturl = PIC_URL;
  }

  ionViewDidLoad() {
    this.userinfo = this.storage.read('userinfo');
    console.log(this.userinfo);
    if (this.userinfo != null) this.getNailerList.user_id = this.userinfo.user_id;
    //this.getNailerList.user_id = this.focusNailerObj.user_id =  this.userinfo.user_id;
    this.events.subscribe('status:created', (status, time) => {
      this.getNailerList.artist_type = status;
      console.log(status,time);

      this.getDetailList();
    });
    this.getDetailList();


  }
  ionViewWillEnter() {
    if(this.userinfo==null){
      this.haveId=false;
    }else{
      this.haveId=true;
    }
  }
  /** 跳转到美图美款*/
  goToProductDetailPage(id,artist_level) {
    console.log(artist_level)
    let modal = this.modalCtrl.create('ImgInfoPage', { 'id': id,'artist_level':artist_level });
    modal.onDidDismiss(data => {
     
    });
    modal.present();
  }
/**下拉刷新*/
  doRefresh(refresher){
console.log(refresher)
    console.log("下拉刷新");

    this.getDetailList()
    setTimeout(() => { 
        console.log('加载完成后，关闭刷新'); 
    refresher.complete();
    let toast = this.toastCtrl.create({
      message: '刷新完成',
      duration: 1500
   });
   toast.present();
    }, 2000);
}
//上滑动加载数据
async doInfinite(infiniteScroll){
 this.getNailerList.page_size+=5;
 console.log(this.getNailerList.page_size)
  let res = await this.nailerservice.getNailerList(this.getNailerList);
  console.log(res.data.length)
  if (res.error_code == 0) {
    this.nailerList = res.data;
    this.nailerList.forEach((e, i) => {
      e.nails.splice(3);
    })
  } else {
    this.nailerList = [];
  }
  setTimeout(() => { 
      // console.log('加载完成后，关闭刷新'); 
      if(res.data.length==this.getNailerList.page_size){
      infiniteScroll.complete();
    //   let toast = this.toastCtrl.create({
    //     message: '加载完成',
    //     duration: 1500
    //  });
    //  toast.present();
    }
      if(res.data.length< this.getNailerList.page_size){
        infiniteScroll.enable(false);
        this.btm=true
      //   let toast = this.toastCtrl.create({
      //     message: '已加载全部',
      //     duration: 1500
      //  });
      //  toast.present();
      }
      if(res.data.length== undefined){
        infiniteScroll.enable(false);
        this.btm=true
   
      }
  }, 1000);
}
  /**跳转到详情页面 */
  gotoNailerDetail(nailerdetail) {
    console.log(nailerdetail)
    let modal = this.modalCtrl.create("NailerDetailPage", nailerdetail);
    modal.onDidDismiss(data => {
      this.getDetailList();
    });
    modal.present();
  }
  // /**关注 */
  obout(stu, artist_id, index) {
    event.stopPropagation();
    console.log("开始点关注");

    console.log(stu, index);

    if (this.userinfo == null) {
      this.showAlert("请先登录");
      return;
    }
    this.nailerList[index].is_focus = stu;
    this.getUserFocus(artist_id);
  }
  // 420100

  
  /**获取美甲师的列表 */
  async getDetailList() {
    console.log(this.getNailerList);
    let res = await this.nailerservice.getNailerList(this.getNailerList);
    // console.log(res);
    console.log(res);
    if (res.error_code == 0) {
      this.nailerList = res.data;
      this.nailerList.forEach((e, i) => {
        e.nails.splice(3);
      })
    } else {
      this.nailerList = [];
    }

  }
  /**关注 */
  async getUserFocus(ids) {
    this.focusNailerObj.focused_id = ids;
    if (this.userinfo == null) {
      this.focusNailerObj.user_id = null;
    } else {
      this.focusNailerObj.user_id = this.userinfo['user_id'];
    }
    let res = await this.nailerservice.getUserFocus(this.focusNailerObj);
    console.log(res);
    if (res.msg == "关注成功") {
      this.msg = "关注成功";
      this.presentToast();
    } else if (res.msg == "取消关注成功") {
      this.msg = "取消关注成功";
      this.presentToast();
    }
    this.getDetailList();

  }
  presentToast() {
    let toast = this.toastCtrl.create({
      message: this.msg,
      duration: 2000,
      position: 'top'
    });
    toast.onDidDismiss(() => {
      console.log('Dismissed toast');
    });

    toast.present();
  }
  //跳转到发布
  add1() {
    let modal = this.modalCtrl.create("JusphPage");
    modal.present();
    // this.navCtrl.push("AddPage")
  }

  goto(){
    this.navCtrl.push("JusphPage");
  }
}
